package it.avantsoft.ls.gl;

import it.avantsoft.ls.gl.data.AddressBook;
import it.avantsoft.ls.gl.message.*;
import org.apache.log4j.Logger;

import java.io.DataOutputStream;
import java.io.OutputStream;

public class SenderMessageToChild {
    private static Logger log = Logger.getLogger(SenderMessageToChild.class);

    private DataOutputStream dataOutputStream;
    private GroupChildHandler child;

    public SenderMessageToChild(GroupChildHandler child, OutputStream output) {
        this.child = child;
        dataOutputStream = new DataOutputStream(output);
    }

    private void log(GroupLeaderMessage msg) {
        log.info("send message " + msg + " to " + child);
    }

    public boolean AB(String groupLeaderId, AddressBook lowerAB) {
        ABMessage message = new ABMessage(dataOutputStream, groupLeaderId, child.getChildId(), lowerAB);
        log(message);
        return message.send();
    }

    public boolean AB(String groupLeaderId, AddressBook lowerAB, AddressBook centerAB) {
        ABMessage message = new ABMessage(dataOutputStream, groupLeaderId, child.getChildId(), lowerAB, centerAB);
        log(message);
        return message.send();

    }

    public boolean newChild(String childId, int port) {
        NewChildMessage message = new NewChildMessage(dataOutputStream, childId, port);
        log(message);
        return message.send();
    }

    public boolean connectToGL(String childId, String groupLeaderHost, int groupLeaderPort) {
        ConnectToGLMessage message = new ConnectToGLMessage(dataOutputStream, childId, groupLeaderHost, groupLeaderPort);
        log(message);
        return message.send();
    }

    public boolean pong() {
        PongMessage message = new PongMessage(dataOutputStream);
        log(message);
        return message.send();
    }

    public boolean horizontalBalance(int loadDifference, GroupChildHandler lessLoaded) {
        HorizontalBalanceMessage message = new HorizontalBalanceMessage(dataOutputStream, loadDifference, lessLoaded.getChildId());
        log(message);
        return message.send();
    }

    public boolean signalBrother(String id, int port) {
        NewBrotherABMessage message = new NewBrotherABMessage(dataOutputStream, id, port);
        log(message);
        return message.send();  //To change body of created methods use File | Settings | File Templates.
    }

    public boolean signalUncle(String id, int port) {
        NewUncleABMessage message = new NewUncleABMessage(dataOutputStream, id, port);
        log(message);
        return message.send();
    }

    public boolean signalNiece(String id) {
        NewNieceABMessage message = new NewNieceABMessage(dataOutputStream, id);
        log(message);
        return message.send();
    }

    public boolean signalDeadNiece(String id) {
        DeadNieceABMessage message = new DeadNieceABMessage(dataOutputStream, id);
        log(message);
        return message.send();
    }

    public boolean signalDeadUncle(String peerID) {
        DeadUncleABMessage message = new DeadUncleABMessage(dataOutputStream, peerID);
        log(message);
        return message.send();
    }


    public boolean signalDeadBrother(String childId) {
        DeadBrotherABMessage message = new DeadBrotherABMessage(dataOutputStream, childId);
        log(message);
        return message.send();
    }

    public boolean sendAckHorizontalBalance(String incomingPeer, String designedGroupLeaderHost, int designedGroupLeaderListeningPort) {
        AckHorizontalBalanceMessage message = new AckHorizontalBalanceMessage(dataOutputStream, incomingPeer, designedGroupLeaderHost, designedGroupLeaderListeningPort);
        log(message);
        return message.send();
    }
}
